package hasthi.actuators;

import hasthi.config.AgentConfig;
import hasthi.config.HostConfig;
import hasthi.config.ResourceProfile;
import hasthi.config.SystemProfileDocument.SystemProfile;

import java.util.HashMap;
import java.util.Map;

import javax.xml.namespace.QName;

public class SystemConfig {
    private Map<QName,ResourceProfile> resourceMap = new HashMap<QName, ResourceProfile>();
    private Map<String,HostConfig> hostMap = new HashMap<String, HostConfig>();
    private Map<String,AgentConfig> agentMap = new HashMap<String, AgentConfig>();
    private SystemProfile profile;
    
    public SystemConfig(SystemProfile profile){
        this.profile = profile;
        if(profile.getResourceArray() != null){
            for(ResourceProfile resourceConfig:profile.getResourceArray()){
                resourceMap.put(resourceConfig.getName(),resourceConfig );
            }
        }
        if(profile.getHostArray() != null){
            for(HostConfig hostConfig:profile.getHostArray()){
                hostMap.put(hostConfig.getName(),hostConfig );
            }
        }
//        for(AgentConfig resourceConfig:profile.getResourceArray()){
//            agentMap.put(resourceConfig.getName(),resourceConfig );
//        }
    }
    
    
    public ResourceProfile getResourceProfile(QName name){
        ResourceProfile resourceProfile = resourceMap.get(name);
        if(resourceProfile == null 
                && (name.getNamespaceURI() == null || name.getNamespaceURI().length() == 0)){
             resourceProfile = resourceMap.get(new QName("http://extreme.indiana.edu/hasthi/config/",name.getLocalPart()));
        }
        return resourceProfile;
    }
    public HostConfig getHostProfile(String name){
        return hostMap.get(name);
    }
    public AgentConfig getAgentProfile(String name){
        return agentMap.get(name);
    }
    
    
    public HostConfig pickAHost(){
        if(hostMap.size() > 0){
            return profile.getHostArray((int)System.currentTimeMillis()%hostMap.size());
        }
        return null;
    }


    public SystemProfile getProfile() {
        return profile;
    }
    
    
}
